// pages/recruit/personal/resume/addworkex/addworkex.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textlimit: 0,
    work:{
      companyName: "",
      startTime: "请选择",
      endTime: "请选择",
      content: ""
    },
    state: "new",//默认新建，进来时带有状态时则变为修改。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('crossdata', function (res) {
      console.log(res)
      that.setData({
        textlimit: res.data.content.length,
        work: res.data,
        state: "edit"
      })
    })
  },
  companyinput: function(e) {
    const that = this;
    that.data.work.companyName = e.detail.value;
    that.setData({
      work: that.data.work,
    })
  },
  begintime: function (e) {
    const that = this;
    that.data.work.startTime = e.detail.value;
    that.setData({
      work: that.data.work,
    })
  },
  endtime: function (e) {
    const that = this;
    that.data.work.endTime = e.detail.value;
    that.setData({
      work: that.data.work,
    })
  },
  textarea: function (e) {
    const that = this;
    if (e.detail.value.length > 0) {
      that.setData({
        ishowtext: ""
      })
    } else {
      that.setData({
        ishowtext: "show"
      })
    }
    that.data.work.content = e.detail.value;
    that.setData({
      work: that.data.work,
      textlimit: e.detail.value.length
    })
  },
  back: function () {
    wx.navigateBack({})
  },
  commit: function () {
    const that = this;
    var box = that.data.work;
    var title;
    if (box.companyName == "") {
      title = "请填写公司名称"
    } else if (box.startTime == "请选择") {
      title = "请选择工作开始时间"
    } else if (box.endTime == "请选择") {
      title = "请选择毕业时间"
    } else if (box.content == ""){
      title = "请填写你的工作经历"
    } else {
      if (that.data.state == "new") {
        box.resumeId = getApp().globalData.resumeId;
        getApp().globalData.post("/api/resume/work-experience", box).then(res => {
          wx.navigateBack();
        })
      } else {
        getApp().globalData.post("/api/resume/work-experience", box).then(res => {
          wx.navigateBack();
        })
      }
      return;
    }
    wx.showToast({
      title: title,
      icon: "none",
      duration: 1000
    })
  }
})